import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

# 加载Excel文件数据
file_similarity = r"C:\Users\zlsjJSSA\Desktop\工作簿1.xlsx"
df_similarity = pd.read_excel(file_similarity, sheet_name='Sheet1')  # 假设最高相似度数据在第一个表单
df_true_label = pd.read_excel(file_similarity, sheet_name='Sheet2')  # 假设true_label数据在第二个表单

# 定义阈值范围
thresholds = [0.6, 0.7, 0.8, 0.9]

# 绘制每个阈值下的混淆矩阵图像
for threshold in thresholds:
    # 应用阈值进行预测
    predicted_labels = np.where(df_similarity["最高相似度"] >= threshold, 1, 0)

    # 计算混淆矩阵
    cm = confusion_matrix(df_true_label["true_label"], predicted_labels)

    # 显示混淆矩阵图像
    disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=[0, 1])
    disp.plot(cmap=plt.cm.Blues, values_format='d')

    # 添加标题
    plt.title(f'Confusion Matrix (Threshold = {threshold})')

    # 保存图片
    plt.savefig(f'confusion_matrix_threshold_{threshold}.png')

    # 显示图像
    plt.show()
